package com.wut.clock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wut.clock.common.constant.ClockConstant;
import com.wut.clock.common.constant.MessageConstant;
import com.wut.clock.common.constant.NotificationConstant;
import com.wut.clock.entity.Message;
import com.wut.clock.entity.Notification;
import com.wut.clock.entity.SysUser;
import com.wut.clock.mapper.MessageMapper;
import com.wut.clock.mapper.NotificationMapper;
import com.wut.clock.mapper.SysUserMapper;
import com.wut.clock.service.INotificationService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 系统消息通知实现类
 * @date 2022/5/31 20:56
 */
@Service
@RequiredArgsConstructor
public class NotificationServiceImpl implements INotificationService {

    private final NotificationMapper notificationMapper;

    private final SysUserMapper userMapper;

    private MessageMapper messageMapper;

    @Override
    public int addNotification(Notification notification) {
        return notificationMapper.insert(notification);
    }

    @Override
    public int deleteNotification(Integer notificationId) {
        return notificationMapper.deleteById(notificationId);
    }

    @Override
    public int updateNotification(Notification notification) {
        return notificationMapper.updateById(notification);
    }

    @Transactional
    @Override
    public int releaseNotification(Integer notificationId) {
        Notification notification = notificationMapper.selectById(notificationId);
        Integer targetType = notification.getTargetType();
        if(targetType.equals(0)){
            // 发给全部用户
            List<SysUser> list = userMapper.selectList(new QueryWrapper<SysUser>());
            // 过滤掉管理员用户
            List<SysUser> userList = list.stream().filter(user -> user.getId() != 1).collect(Collectors.toList());
            userList.forEach(user->{
                Message message = new Message();
                message.setUserId(user.getId());
                message.setFromUserId(ClockConstant.ADMIN);
                message.setType(MessageConstant.Type.NOTIFICATION.getCode());
                message.setContent(notification.getContent());
                messageMapper.insert(message);
            });
        }else {
            // 解析出用户id
            String[] stringUserIdList = notification.getTargetUserId().split(",");
            for (String stringUserId : stringUserIdList) {
                int userId = Integer.parseInt(stringUserId);
                Message message = new Message();
                message.setUserId(userId);
                message.setFromUserId(ClockConstant.ADMIN);
                message.setType(MessageConstant.Type.NOTIFICATION.getCode());
                message.setContent(notification.getContent());
                messageMapper.insert(message);
            }
        }
        // 将发布状态设置为1
        notification.setStatus(NotificationConstant.Status.CHECKING.getCode());
        return notificationMapper.updateById(notification);
    }

    @Override
    public List<Notification> getNotificationList(Integer pageNum, Integer pageSize, Integer targetType, String content, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Notification> wrapper = new QueryWrapper<>();
        if(targetType != null) wrapper.eq("target_type",targetType);
        if(StringUtils.isNotBlank(content)) wrapper.like("content",content);
        if(status != null) wrapper.eq("status",status);
        return notificationMapper.selectList(wrapper);
    }

    @Override
    public Notification getNotificationDetail(Integer notificationId) {
        return notificationMapper.selectById(notificationId);
    }
}
